package com.bhzyxy.love;

import android.graphics.drawable.Drawable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.RelativeLayout;

import androidx.annotation.NonNull;
import androidx.appcompat.widget.AppCompatImageView;
import androidx.appcompat.widget.AppCompatTextView;
import androidx.recyclerview.widget.RecyclerView;

import com.bumptech.glide.Glide;

import java.util.ArrayList;
import java.util.ResourceBundle;


public class LoveAdapter extends RecyclerView.Adapter<LoveAdapter.ViewHolder> {

    private final ArrayList<LoveData> arrayList;
    private OnItemClickListener onItemClickListener = null;
    private ResourceBundle jsonObject;


    public LoveAdapter(ArrayList<LoveData> arrayList) {

        this.arrayList = arrayList;

    }

    @Override
    public void onBindViewHolder(@NonNull ViewHolder holder, int position) {
        LoveData bean = arrayList.get(position);
        if (isImagePosition(position)) {
            String url = jsonObject.getString("avatar_url");
            Glide.with(holder.avatarImageView)
                    .load(url)
                    .into(holder.avatarImageView);
        } else {
            Glide.with(holder.avatarImageView).clear(holder.avatarImageView);
            Drawable specialDrawable = null;
            holder.avatarImageView.setImageDrawable(specialDrawable);
        }
        holder.textTextView.setText(bean.getText());

        holder.mainRelativeLayout.setOnClickListener(view -> {
                if (onItemClickListener != null) {
                onItemClickListener.onClick(position, bean);
            }
        });

        holder.deleteTextView.setOnClickListener(view -> {
            if (onItemClickListener != null) {
                onItemClickListener.onDelete(position, bean);
            }
        });

    }

    private boolean isImagePosition(int position) {
        return false;
    }

    @NonNull
    @Override
    public ViewHolder onCreateViewHolder(@NonNull ViewGroup group, int viewType) {

        View view = LayoutInflater.from(group.getContext()).inflate(R.layout.item_love, group, false);
        return new ViewHolder(view);

    }

    @Override
    public int getItemCount() {

        return arrayList.size();

    }

    @SuppressWarnings("unused")
    public void setOnItemClickListener(OnItemClickListener listener) {

        this.onItemClickListener = listener;

    }

    public void setJsonObject(ResourceBundle jsonObject) {
        this.jsonObject = jsonObject;
    }


    @SuppressWarnings("unused")
    public interface OnItemClickListener {

        @SuppressWarnings({"unused", "EmptyMethod"})
        void onClick(int position, LoveData bean);

        @SuppressWarnings({"unused", "EmptyMethod"})
        void onDelete(int position, LoveData bean);

    }

    static class ViewHolder extends RecyclerView.ViewHolder {

        private final RelativeLayout mainRelativeLayout;
        private final AppCompatTextView textTextView;
        private final AppCompatTextView deleteTextView;
        public AppCompatImageView avatarImageView;

        private ViewHolder(View view) {

            super(view);
            mainRelativeLayout = view.findViewById(R.id.mainRelativeLayout);
            AppCompatImageView avatarImageView = view.findViewById(R.id.avatarImageView);
            textTextView = view.findViewById(R.id.textTextView);
            deleteTextView = view.findViewById(R.id.deleteTextView);

        }

    }

}
